package com.wiiss.ams.cron.task;

import cn.hutool.cron.task.Task;
import com.wiiss.ams.cron.Cron;
import com.wiiss.ams.cron.task.action.CronAction;
import com.wiiss.ams.util.Executor;

import java.util.List;

public class ScheduleTask implements Task {

    private Cron cron;

    private CronAction[] cronActions;

    public static ScheduleTask of(String cronExpression, CronAction... cronActions) {
        ScheduleTask scheduleTask = new ScheduleTask(cronActions);
        scheduleTask.cron = Cron.of(cronExpression);
        return scheduleTask;
    }

    private ScheduleTask(CronAction[] scheduleTaskActions) {
        super();
        this.cronActions = scheduleTaskActions;
    }

    @Override
    public void execute() {
        cron.trigger(); // 触发cron对象更新相关数据

        List.of(cronActions).forEach(v -> {
            v.setCron(cron);
            Executor.getInstance().execute(v);
        });
    }

    public Cron getCron() {
        return cron;
    }

}
